<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>组件</title>
    <script src="./vue.js"></script>
</head>

<body>
    <div id='app'>
        <input type="text" v-model="inputValue" />
        <button v-on:click="headleBtnClick">提交</button>
        <ul>
            <!-- <li v-for="item in list">{{item}}</li> -->
            <todo-item v-bind:content="item" v-bind:index="index" v-for="(item,index) in list" @delete="handleItemDelete">

            </todo-item>
        </ul>
    </div>

    <script>
        //创建全局组件
        // Vue.component("todo-item", {
        //     props: ['content'],
        //     template: "<li>{{content}}</li>"
        // })

        //局部注册-子组件
        var TodoItem = {
            props: ['content', 'index'],
            template: "<li @click='handleItemClick'>{{content}}</li>",
            methods: {
                handleItemClick: function () {
                    this.$emit("delete", this.index);//点击向外触发事件---监听事件
                }
            }
        }

        //父组件
        var app = new Vue({
            el: '#app',
            components: {
                TodoItem: TodoItem
            },
            data: {
                list: [],
                inputValue: ''
            },
            methods: {
                headleBtnClick: function () {
                    this.list.push(this.inputValue)
                    this.inputValue = ''
                    // alert(this.inputValue);
                },
                handleItemDelete: function (index) {
                    this.list.splice(index, 1)
                }
            }
        })
    </script>
</body>

</html>